<?php
if (! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * model基类重写
 * User: zyw
 * Date: 16/7/26
 * Time: 下午2:16
 */
class MY_Model extends CI_Model{

    protected $table;

    public function __construct(){
        $sql = "SET NAMES utf8mb4;";
        $this->db->query($sql);
        parent::__construct();
    }

    /**
     * 插入一条数据
     * @param array $data
     * @return mixed
     * @throws Exception
     */
    public function insert_one(array $data){
        try{
            $this->db->insert($this->table,$data);
            $result = $this->db->insert_id();
            return $result;
        }catch(Exception $e){
            throw new Exception($e->getMessage());
        }
    }

    /**
     * 分页列表
     * @param array $where
     * @param int $page
     * @param int $pagesize
     * @param array $orderBy
     * @return mixed
     */
    public function getList($where = array(), $page = 1, $pagesize = 20, $orderBy = array()){
        if(!empty($where)){
            foreach($where as $k => $v){
                if(is_array($v)){
                    $this->db->where_in($k, $v);
                }else{
                    $this->db->where($k, $v);
                }
            }
        }
        if(!empty($orderBy)){
            foreach($orderBy as $key => $value){
                $this->db->order_by($key, $value);
            }
        }
        if($page == 0){
            return $this->db->get($this->table)->result_array();
        }else{
            $index = $page - 1 < 0 ? 0 : ($page - 1) * $pagesize;
            return $this->db->limit($pagesize, $index)->get($this->table)->result_array();
        }
    }

    /**
     * 分页列表数量
     * @param array $where
     * @return mixed
     */
    public function getListNum($where = array()){
        if(!empty($where)){
            foreach($where as $k => $v){
                if(is_array($v)){
                    $this->db->where_in($k, $v);
                }else{
                    $this->db->where($k, $v);
                }
            }
        }
        $this->db->from($this->table);
        return $this->db->count_all_results();
    }

    /**
     * 查询一条数据
     * @param $where array
     * @return mixed
     * @throws Exception
     */
    public function query_one(array $where){
        try{
            $this->db->where($where);
            return $this->db->get($this->table)->row_array();
        }catch(Exception $e){
            throw new Exception($e->getMessage());
        }
    }

    /**
     * 查询结果集
     * @param array $where
     * @return mixed
     * @throws Exception
     */
    function querylist(array $where){
        try{
            return $this->db->where($where)->get($this->table)->result_array();

        }catch(Exception $e){
            throw new Exception($e->getMessage());
        }
    }

    /**
     * 查询总记录数量
     * @param array $where
     * @return mixed
     * @throws Exception
     */
    function query_count(array $where){
        try{
            $this->db->where($where);
            $this->db->from($this->table);
            return $this->db->count_all_results();

        }catch(Exception $e){
            throw new Exception($e->getMessage());
        }

    }

    /**
     * 删除数据
     * @param $where array
     * @return int 返回影响的行数
     * @throws Exception
     */
    function del_data($where){
        try{
            $this->db->delete($this->table,$where);
            return $this->db->affected_rows(); //返回影响的行数
        }catch(Exception $e){
            throw new Exception($e->getMessage());
        }

    }

    /**
     * 修改数据
     * @param $data array
     * @param $where array
     * @return int 返回影响的行数
     * @throws Exception
     */
    function update_data($data, $where){
        try{
            $this->db->update($this->table, $data, $where);
            return $this->db->affected_rows(); //返回影响的行数
        }catch(Exception $e){
            throw new Exception($e->getMessage());
        }
    }
    /**
     * 查询一条数据
     * @param $sql string
     * @return array
     */
    public function query_sql_one($sql){
        try{
            return $this->db->query($sql)->row_array();
        }catch(Exception $e){
            throw new Exception($e->getMessage());
        }
    }

    /**
     * 查询结果集
     * @param $sql
     * @return array
     */
    function query_sql_list($sql){
        try{
            $result =array();
            $query = $this->db->query($sql);
            if($query){
                foreach($query->result_array() as $row){
                    $result[] = $row ;
                }
            }
            return $result ;
        }catch(Exception $e){
            throw new Exception($e->getMessage());
        }
    }

    /**
     * 查询总记录数量
     * @param $sql
     * @return int
     */
    function query_sql_count($sql){
        try{
            $query = $this->db->query($sql);
            $num_array = $query->result_array();
            $num = 0 ;
            if(isset($num_array[0]) && !empty($num_array[0])){
                foreach ($num_array[0] as $k=>$v){
                    $num = $v ;
                    break ;
                }
            }
            return $num ;
        }catch(Exception $e){
            throw new Exception($e->getMessage());
        }

    }

    /**
     * 触发任务操作方法
     * name: taskEventApi
     * @param string $code
     * @param array $data
     * @return bool
     * @Author:taiyang
     */
    public function taskEventApi($code='',$data=array()){
        if(empty($code) || empty($data)){return false;}
        if(empty($data['uid'])){return false;}
        $token=onRequest();
        $post=array(
//            'data'=>json_encode($data)
        );
//        $url='http://task.task/index.php?c=prize&m=event&code='.$code.'&token='.$token.'&data='.json_encode($data);
        $url='http://task.nuanguang.vip/index.php?c=prize&m=event&code='.$code.'&token='.$token.'&data='.json_encode($data);
        $res=curl_request($url,$post);
        return true;
    }

}